package com.orange.off.cloud.cloudoff.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.events.ChangePasswordEvent;
import com.orange.off.cloud.cloudoff.fragments.ClientFragment;
import com.orange.off.cloud.cloudoff.fragments.PersonalFragment;
import com.orange.off.cloud.cloudoff.fragments.RealEstateFragment;
import com.orange.off.cloud.cloudoff.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.real_estate_image)
    ImageView realEstateImage;
    @Bind(R.id.real_estate_text)
    TextView realEstateText;
    @Bind(R.id.real_estate_relative)
    RelativeLayout realEstateRelative;
    @Bind(R.id.client_image)
    ImageView clientImage;
    @Bind(R.id.client_text)
    TextView clientText;
    @Bind(R.id.client_relative)
    RelativeLayout clientRelative;
    @Bind(R.id.persona_image)
    ImageView personaImage;
    @Bind(R.id.persona_text)
    TextView personaText;
    @Bind(R.id.persona_relative)
    RelativeLayout personaRelative;
    private FragmentManager manager;
    /**
     * 楼盘资源real_estate
     */
    private RealEstateFragment realEstateFragment;
    public static final int INDEX_REAL_ESTATE = 0;
    /**
     * 我的客户client
     */
    private ClientFragment clientFragment;
    public static final int INDEX_CLIENT = 1;
    /**
     * 个人中心
     */
    private PersonalFragment personalFragment;
    public static final int INDEX_PERSONAL = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        manager = getSupportFragmentManager();
        int type = getIntent().getIntExtra("type", INDEX_REAL_ESTATE);
        setTabSelection(type);
    }

    @OnClick({R.id.real_estate_relative, R.id.client_relative, R.id.persona_relative})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.real_estate_relative:
                setTabSelection(INDEX_REAL_ESTATE);
                break;
            case R.id.client_relative:
                setTabSelection(INDEX_CLIENT);
                break;
            case R.id.persona_relative:
                setTabSelection(INDEX_PERSONAL);
                break;
        }
    }

    /**
     * 根据传入index来设置选中tab页
     *
     * @param index tab页对应的下标
     */
    private void setTabSelection(int index) {
        FragmentTransaction transaction = manager.beginTransaction();
        hideAllFragment(transaction);
        clearSelection();
        switch (index) {
            case INDEX_REAL_ESTATE:
                realEstateImage.setImageResource(R.mipmap.table_real_estate_orange);
                realEstateText.setTextColor(getResources().getColor(R.color.orange));
                if (realEstateFragment == null) {
                    realEstateFragment = new RealEstateFragment();
                    transaction.add(R.id.content, realEstateFragment);
                } else {
                    transaction.show(realEstateFragment);
                }
                break;
            case INDEX_CLIENT:
                clientImage.setImageResource(R.mipmap.table_client_orange);
                clientText.setTextColor(getResources().getColor(R.color.orange));
                if (clientFragment == null) {
                    clientFragment = new ClientFragment();
                    transaction.add(R.id.content, clientFragment);
                } else {
                    transaction.show(clientFragment);
                }
                break;
            case INDEX_PERSONAL:
                personaImage.setImageResource(R.mipmap.table_personal_orange);
                personaText.setTextColor(getResources().getColor(R.color.orange));
                if (personalFragment == null) {
                    personalFragment = new PersonalFragment();
                    transaction.add(R.id.content, personalFragment);
                } else {
                    transaction.show(personalFragment);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (realEstateFragment != null)
            fragmentTransaction.hide(realEstateFragment);
        if (clientFragment != null)
            fragmentTransaction.hide(clientFragment);
        if (personalFragment != null)
            fragmentTransaction.hide(personalFragment);
    }

    /**
     * 清空tab选中状态恢复原始值
     */
    private void clearSelection() {
        realEstateImage.setImageResource(R.mipmap.table_real_estate_grey);
        realEstateText.setTextColor(getResources().getColor(R.color.text_color));
        clientImage.setImageResource(R.mipmap.table_client_grey);
        clientText.setTextColor(getResources().getColor(R.color.text_color));
        personaImage.setImageResource(R.mipmap.table_personal_grey);
        personaText.setTextColor(getResources().getColor(R.color.text_color));
    }
    /**
     * EventBus：密码修/退出账号改成功事件通知(退出该界面重新登录)
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdatePwdEvent(ChangePasswordEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    private long waitTime = 2000;
    private long touchTime = 0;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            exit();
            return true;
        }
        return false;
    }

    private void exit() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - touchTime >= waitTime) {
            ToastUtils.show(this, R.string.exit_show);
            touchTime = currentTime;
        } else {
            this.finish();
        }
    }
}
